﻿#if NETFX || NETCORE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Apewer;

namespace Apewer.Surface
{

    /// <summary></summary>
    public partial class BlockLog : BaseControl
    {

        private DataGridView _table = new DataGridView();

        private Panel _tipwall = new Panel();
        private TextBox _tiptext = new TextBox();

        /// <summary></summary>
        public BlockLog()
        {
            BackColor = FormsUtility.GraceBorder;
            Padding = new Padding(1);

            Controls.Add(_table);
            Controls.Add(_tipwall);
            _tipwall.Controls.Add(_tiptext);

            _tipwall.Visible = false;
            _tipwall.Dock = DockStyle.Fill;
            _tipwall.Padding = new Padding(40);
            _tipwall.BackColor = FormsUtility.White;

            _tiptext.Dock = DockStyle.Fill;
            _tiptext.Multiline = true;
            //_tiptext.ReadOnly = true;
            _tiptext.BorderStyle = BorderStyle.None;
            _tiptext.BackColor = _tipwall.BackColor;
            _tiptext.ScrollBars = ScrollBars.None;

            _table.Dock = DockStyle.Fill;
            _table.Font = FormsUtility.DefaultFont; ;
            _table.ColumnHeadersHeight = 32;
            _table.RowHeadersVisible = false;
            _table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            _table.MultiSelect = false;
            //_table.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            _table.BackgroundColor = FormsUtility.White;
            _table.GridColor = FormsUtility.GraceBorder;

            _table.BorderStyle = BorderStyle.None;
            _table.CellBorderStyle = DataGridViewCellBorderStyle.None;
            _table.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            _table.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            _table.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            _table.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

            _table.ReadOnly = true;
            _table.AllowUserToAddRows = false;
            _table.AllowUserToDeleteRows = false;
            _table.AllowUserToOrderColumns = false;
            _table.AllowUserToResizeColumns = true;
            _table.AllowUserToResizeRows = false;

            _table.RowsDefaultCellStyle.BackColor = Color.White;
            _table.RowsDefaultCellStyle.NullValue = "";
            _table.RowsDefaultCellStyle.SelectionBackColor = FormsUtility.GraceWall;
            _table.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.False;

            _table.Columns.Add("_clock", "时间");
            _table.Columns.Add("_text", "内容");
            _table.Columns["_clock"].Width = 150;
            _table.Columns["_text"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            _table.ColumnHeadersVisible = false;

            _table.CellDoubleClick += Event_Table_DoubleClick;
            _tipwall.DoubleClick += Event_TipWall_DoubleClick;
            _tiptext.DoubleClick += Event_TipText_DoubleClick;
        }

        /// <summary></summary>
        public new void Dispose()
        {
            _tipwall.Controls.Clear();
            Controls.Clear();
            if (_table != null) _table.Dispose();
            if (_tiptext != null) _tiptext.Dispose();
            if (_tipwall != null) _tipwall.Dispose();
            base.Dispose();
        }

        private void Event_TipText_DoubleClick(object sender, EventArgs e)
        {
            _tipwall.Visible = false;
            _table.Visible = true;
        }

        private void Event_TipWall_DoubleClick(object sender, EventArgs e)
        {
            _tipwall.Visible = false;
            _table.Visible = true;
        }

        private void Event_Table_DoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            {
                _table.Visible = false;
                _tipwall.Visible = true;
                _tiptext.Text = _table[e.ColumnIndex, e.RowIndex].Value.ToString();
                _tiptext.ForeColor = _table[e.ColumnIndex, e.RowIndex].Style.ForeColor;
            }
        }

        /// <summary></summary>
        public void Write(string text = "") { Write(text, ForeColor); }

        /// <summary></summary>
        public void Blue(string text = "") { Write(text, FormsUtility.Blue); }

        /// <summary></summary>
        public void Purple(string text = "") { Write(text, FormsUtility.Purple); }

        /// <summary></summary>
        public void Orange(string text = "") { Write(text, FormsUtility.Orange); }

        /// <summary></summary>
        public void Black(string text = "") { Write(text, FormsUtility.Black); }

        /// <summary></summary>
        public void Green(string text = "") { Write(text, FormsUtility.Green); }

        /// <summary></summary>
        public void Red(string text = "") { Write(text, FormsUtility.Red); }

        /// <summary></summary>
        public void Gray(string text = "") { Write(text, FormsUtility.GraceLocked); }

        /// <summary></summary>
        public void Error(string text = "") { Red(text); }

        /// <summary></summary>
        public void Write(string text, Color color)
        {
            string t = string.IsNullOrEmpty(text) ? " " : text;
            try
            {
                Invoke(new Invoker(delegate ()
                {
                    int i = _table.Rows.Add(ClockUtility.LucidNow, t);
                    var c = (color == null) ? FormsUtility.Black : color;
                    _table.Rows[i].Height = 24;
                    _table.Rows[i].Cells[0].Style.Padding = new Padding(6, 0, 4, 0);
                    _table.Rows[i].Cells[0].Style.ForeColor = FormsUtility.GraceLocked;
                    _table.Rows[i].Cells[0].Style.SelectionForeColor = FormsUtility.GraceLocked;
                    _table.Rows[i].Cells[1].Style.ForeColor = c;
                    _table.Rows[i].Cells[1].Style.SelectionForeColor = c;
                    _table.CurrentCell = _table[0, i];
                }));
            }
            catch { }
        }

        /// <summary></summary>
        public void Clean()
        {
            _table.Rows.Clear();
        }

        /// <summary></summary>
        public void Clear()
        {
            Clean();
        }

        /// <summary></summary>
        public void ShowMilli()
        {
            _table.Columns["_clock"].Width = 160;
        }

    }

}

#endif
